using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

namespace Script
{
    public partial class FightMainUI
    {
        public Button 重发卡牌;
        public Button[] 迷宫牌4张的按钮;

        public static FightMainUI Instance=null;
        private void Awake()
        {
            Instance=this;
            重发卡牌.onClick.AddListener(从发牌);
            for (int i = 0; i < 迷宫牌4张的按钮.Length; i++)
            {
                var i1 = i;
                迷宫牌4张的按钮[i].onClick.AddListener(()=>按下迷宫牌(i1));
            }
        }

        private async void 按下迷宫牌(int i)
        {
            await Task.Delay(200);
            Debug.LogError("按下了 "+ Enum.GetName(typeof(MiGongCard),_targetList[i]));
            migongCardRect.gameObject.SetActive(false);

            switch ((MiGongCard)_targetList[i])
            {
                
                case MiGongCard.怪物:
                    //随机几个怪物
                    int Mcount = Random.Range(1, 4);
                    await MonsterUI.instance.StartFightUiAnim(Mcount);
                    //开始战斗逻辑?
                    await Task.Delay(1000);
                    break;
                
                
                
                case MiGongCard.宝箱:
                
                    
                
                    break;
                
                case MiGongCard.事件:
                
                    
                
                    break;
                case MiGongCard.陷阱:
                
                    
                
                    break;
                case MiGongCard.恢复状态:
                
                    
                
                    break;
                case MiGongCard.赌场:
                
                    
                
                    break;
                case MiGongCard.中立npc:
                
                    
                
                    break;
                case MiGongCard.精英怪:
                
                    
                
                    break;
                default: Debug.LogError("无法解析"+Enum.GetName(typeof(MiGongCard),_targetList[i]));
                    break;
            }
        }

        private void 从发牌()
        {
            StartCoroutine(PushMiGongCard());
        }
    }

    public partial class FightMainUI : MonoBehaviour
    {
        /// <summary>
        /// 关卡牌资源
        /// </summary>
        public Sprite[] guankaSprites;
    

        public RectTransform migongCardRect;
        private readonly List<int> _targetList = new List<int>(4);
        //发迷宫卡
        public IEnumerator PushMiGongCard()
        {
            //置空数据
            _targetList.Clear();
        
            //随机卡牌
            var miGongCardArray = Enum.GetValues(typeof(MiGongCard));
        
            while (_targetList.Count<4)
            {
                _targetList.Add(Random.Range(0,miGongCardArray.Length));
            }
        
        
            var btns = migongCardRect.GetComponentsInChildren<Button>().ToList();
            migongCardRect.gameObject.SetActive(false);
            yield return new WaitForSeconds(0.1f);

            for (int i = 0; i < _targetList.Count; i++)
            {
                //设置颜色
                //设置图片
                var img = btns[i].transform.GetChild(1).GetComponent<Image>();
                img.sprite = guankaSprites[_targetList[i]];
                //设置描述
                btns[i].transform.GetChild(0).GetComponent<Text>().text =Enum.GetName(typeof(MiGongCard),_targetList[i]);
            }
        
            //启动动画
            migongCardRect.parent.GetComponent<Animator>().SetTrigger("migongCard" );
            migongCardRect.gameObject.SetActive(true);
            yield return new WaitForSeconds(0.5f);
        }
    }
}